<?php
require_once dirname(__FILE__) . '/Database.class.php';


define("SMALLTIME", "SmallTime");
define("BIGTIME", "BigTime");

define("Table_NodeArray", "nodearray");
define("Table_SensorData", "sensordata");
define("Table_NodeLocalInfo", "nodelocalinfo");
define("COLUMN_Index", "_index");
define("COLUMN_Gateway", "网关ID");
define("COLUMN_NodeName", "节点ID");
define("COLUMN_NodeState", "节点状态");
define("COLUMN_Time", "时间");
define("COLUMN_BatteryLevel", "电量");
define("COLUMN_SensorNum", "数量");
define("COLUMN_SensorType0", "类型0");
define("COLUMN_SensorData0", "数据0");
define("COLUMN_SensorType1", "类型1");
define("COLUMN_SensorData1", "数据1");
define("COLUMN_SensorType2", "类型2");
define("COLUMN_SensorData2", "数据2");
define("COLUMN_SensorType3", "类型3");
define("COLUMN_SensorData3", "数据3");
define("COLUMN_SensorType4", "类型4");
define("COLUMN_SensorData4", "数据4");
define("COLUMN_SensorType5", "类型5");
define("COLUMN_SensorData5", "数据5");
define("COLUMN_SensorType6", "类型6");
define("COLUMN_SensorData6", "数据6");
define("COLUMN_SensorType7", "类型7");
define("COLUMN_SensorData7", "数据7");
define("COLUMN_SensorType8", "类型8");
define("COLUMN_SensorData8", "数据8");
define("COLUMN_SensorType9", "类型9");
define("COLUMN_SensorData9", "数据9");
define("COLUMN_Area", "Area");
define("COLUMN_LocalInfo", "LocalInfo");
define("COLUMN_SensorTypeArray", "类型数组");
global $TableSensorDataHeaderArray;
$TableSensorDataHeaderArray = array(
    COLUMN_Index, COLUMN_Gateway, COLUMN_NodeName, COLUMN_Time,
    COLUMN_BatteryLevel, COLUMN_SensorNum, COLUMN_SensorType0, COLUMN_SensorData0,
    COLUMN_SensorType1, COLUMN_SensorData1, COLUMN_SensorType2, COLUMN_SensorData2,
    COLUMN_SensorType3, COLUMN_SensorData3, COLUMN_SensorType4, COLUMN_SensorData4,
    COLUMN_SensorType5, COLUMN_SensorData5, COLUMN_SensorType6, COLUMN_SensorData6,
    COLUMN_SensorType7, COLUMN_SensorData7, COLUMN_SensorType8, COLUMN_SensorData8,
    COLUMN_SensorType9, COLUMN_SensorData9
);

global $TableNodeArrayHeaderArray;
$TableNodeArrayHeaderArray = array(
    COLUMN_Index, COLUMN_Gateway, COLUMN_NodeName, COLUMN_NodeState,
    COLUMN_SensorNum, COLUMN_SensorTypeArray
);

global $TableNodeLocalInfoHeaderArray;
$TableNodeLocalInfoHeaderArray = array(
    COLUMN_Index, COLUMN_Gateway, COLUMN_NodeName, COLUMN_Area, COLUMN_LocalInfo
);



sqlConnect();
global $tablesArray;

/**
 * 获取DB对象
 *
 * @param string $tablename
 *            表名字
 * @return Database db对象
 */
function TableArray($tablename)
{
    global $tablesArray;
    if (isset($tablesArray[$tablename])) {
        return $tablesArray[$tablename];
    }
    return false;
}

/**
 *
 * @param string $tablename
 *            表名字
 * @param Database $dbobj
 */
function registerTable($tablename, $dbobj)
{
    global $tablesArray;
    $tablesArray[$tablename] = $dbobj;
}

function sqlConnect()
{
    global $_GCONFIG;
    connectTo($_GCONFIG["db"]["host"], $_GCONFIG["db"]["user"], $_GCONFIG["db"]["passwd"], $_GCONFIG["db"]["dbname"]);
}

function connectTo($host, $username, $password, $dbname)
{
    try {
        global $dbConn;

        $dbConn = new mysqli($host, $username, $password, $dbname);
        if ($dbConn->connect_errno) {
            die("数据库链接失败：" . $dbConn->connect_error);
        }
        $dbConn->select_db($dbname) or die("选择数据库失败：" . $dbConn->error);
        $dbConn->set_charset("utf8") or die("设置字符集失败：" . $dbConn->error);
    } catch (Exception $e) {
        die("出现了未知错误：" . $e->__toString());
    }
}

function getConnection()
{
    global $dbConn;
    return $dbConn;
}

function query($sql)
{
    global $dbConn;
    $result = $dbConn->query($sql);
    if (!$result) {
        debug();
        printf("Error: %s\n", $dbConn->error);
        exit();
    }
    return $result;
}
